﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormCommon
{
    public partial class frmGridFilter : Form
    {
        public frmGridFilter()
        {
            InitializeComponent();
        }

        private DataTable m_DataTable;

        private string sCondition;

        public bool Show_Form(DataTable dt)
        {
            //
            string sFilter = dt.DefaultView.RowFilter;
            DataTable dtCondition = new DataTable();
            dtCondition.Columns.Add("Column_Name", typeof(string));
            dtCondition.Columns.Add("Column_Criteria", typeof(string));
            dtCondition.Columns[1].AllowDBNull = false;
            if (!string.IsNullOrEmpty(sFilter))
            {
                sFilter = sFilter.Replace(" AND ", Globals.vbTab);
                foreach (string sCondition2 in sFilter.Split(new string[]{Globals.vbTab},StringSplitOptions.None))
                {
                    sCondition = sCondition2.Trim();
                    DataRow dr = dtCondition.NewRow();
                    int i = grdTableSetting.Rows.Add();
                    int iPos = sCondition.IndexOf(" ");
                    string sFieldName = sCondition.Substring(0, iPos);
                    string sCriteria = sCondition.Substring(iPos + 1);
                    if (sCriteria.StartsWith("="))
                        sCriteria = sCriteria.Substring(1);
                    if (sCriteria.StartsWith("'") && sCriteria.EndsWith("'"))
                    {
                        sCriteria = sCriteria.Substring(1);
                        sCriteria = sCriteria.Substring(0, sCriteria.Length - 1);
                    }
                    dr["Column_Name"] = sFieldName;
                    dr["Column_Criteria"] = sCriteria;
                    dtCondition.Rows.Add(dr);
                }
            }
            DataGridHandle dh = new DataGridHandle(grdTableSetting);
            dh.AutoRequired = true;

            grdTableSetting.Columns[0].DataPropertyName = "Column_Name";
            grdTableSetting.Columns[1].DataPropertyName = "Column_Criteria";
            grdTableSetting.AutoGenerateColumns = false;
            grdTableSetting.DataSource = dtCondition;
            m_DataTable = dt;
            return this.ShowDialog() == DialogResult.OK;
        }

        private void cmdOK_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                sCondition = "";
                foreach (DataGridViewRow row in grdTableSetting.Rows)
                {
                    if (row.IsNewRow)
                        continue;
                    if (!string.IsNullOrEmpty(sCondition))
                    {
                        sCondition += " AND ";
                    }
                    else
                    {
                        sCondition += "";
                    }
                    string sFieldName = Globals.Object_GetValue<string>(row.Cells["Column_Name"].Value);
                    string sCriteria = Globals.Object_GetValue<string>(row.Cells["Column_Criteria"].Value);
                    sCriteria = sCriteria.Trim();
                    if (sCriteria.StartsWith("=") || sCriteria.StartsWith("IN", StringComparison.CurrentCultureIgnoreCase) || sCriteria.StartsWith("LIKE", StringComparison.CurrentCultureIgnoreCase) || sCriteria.StartsWith(">", StringComparison.CurrentCultureIgnoreCase) || sCriteria.StartsWith("<", StringComparison.CurrentCultureIgnoreCase) || sCriteria.StartsWith("BETWEEN", StringComparison.CurrentCultureIgnoreCase) || sCriteria.StartsWith(">=", StringComparison.CurrentCultureIgnoreCase) || sCriteria.StartsWith("<=", StringComparison.CurrentCultureIgnoreCase) || sCriteria.StartsWith("<>", StringComparison.CurrentCultureIgnoreCase))
                    {
                    }
                    else
                    {
                        if (!(sCriteria.StartsWith("'") & sCriteria.EndsWith("'")))
                        {
                            sCriteria = "='" + sCriteria + "'";
                        }
                        else
                        {
                            sCriteria = "=" + sCriteria;
                        }
                    }
                    sCondition += sFieldName + " " + sCriteria;
                }
                try
                {
                    m_DataTable.DefaultView.RowFilter = sCondition;
                }
                catch
                {
                    FormGlobals.Message_Information("Invalid condition, please check condition syntax");
                    return;
                }
                this.Tag = sCondition;
                this.DialogResult = DialogResult.OK;
                this.Hide();
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Error(ex);
            }
        }

        private void cmdCancel_Click(object sender, System.EventArgs e)
        {
            this.Hide();
        }
    }
}
